//
//  GMH_HistoryViewController.m
//  
//
//  Created by 郭明皓 on 15/11/4.
//
//

#import "GMH_HistoryViewController.h"
#import "GMHSurroundModel.h"
#import "Sqlite.h"
#import "TuijianCell.h"
#import <UIImageView+WebCache.h>
#import "GMHFindDetailViewController.h"
#import "GMHSurroundModel.h"
#import "DKNightVersion.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface GMH_HistoryViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, retain)UILabel *backLabel;
@property (nonatomic, retain)UIImageView *backImage;

@end

@implementation GMH_HistoryViewController

- (void)dealloc
{
    [_backLabel release];
    [_backImage release];
    [_tableview release];
    [_dataArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清除记录" style:UIBarButtonItemStyleDone target:self action:@selector(clear)];
    
    self.title = @"浏览历史";
    //    [self.tableview registerClass:[TuijianCell class] forCellReuseIdentifier:@"tableView"];
    
    [self createtableview];
    self.dataArr = [NSMutableArray array];
    
    
}
- (void)createtableview
{
    self.tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [_tableview release];
    self.tableview.nightBackgroundColor = UIColorFromRGB(0x343434);
    self.tableview.nightSeparatorColor = UIColorFromRGB(0x313131);
    
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.rowHeight = 100;
}
//清除功能
- (void)clear
{
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"%@", sandBox);
    NSString *dbPath = [sandBox stringByAppendingPathComponent:@"History.db"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    
    NSLog(@"%@", dbPath);
    if ([db open]) {
        NSString *deletesql = [NSString stringWithFormat:@"delete from  History"];
        NSLog(@"%@", deletesql);
        [db executeUpdate:deletesql];
        [_dataArr removeAllObjects];
        [self.tableview reloadData];

    }
    NSLog(@"%@", dbPath);
}

- (void)createDb
{
    [_dataArr removeAllObjects];
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *dbPath = [sandBox stringByAppendingPathComponent:@"History.db"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    NSLog(@"%@", dbPath);
    
    if ([db open]) {
        
        NSString *sql = [NSString stringWithFormat:@"select * from History order by Ids desc"];
        FMResultSet *s = [db executeQuery:sql];
        while ([s next]) {
            GMHSurroundModel *model = [[GMHSurroundModel alloc]init];
            model.productId = [s stringForColumn:@"productId"];
            model.mImageUrl = [s stringForColumn:@"mImageUrl"];
            model.productName = [s stringForColumn:@"productName"];
            model.productTitleContent = [s stringForColumn:@"productTitleContent"];
            model.price = [NSNumber numberWithInt:[s intForColumn:@"price"]];
            model.retailPrice = [NSNumber numberWithInt:[s intForColumn:@"tetailPrice"]];
            model.saledCount = [NSNumber numberWithInt:[s intForColumn:@"saledCount"]];
            [self.dataArr addObject:model];
            [model release];
        }
        if (self.dataArr.count > 0) {
            [self.tableview reloadData];
        }
        else
        {
            [self.tableview removeFromSuperview];
            self.backLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.center.x - WIDTH / 6, HEIGHT / 20 * 9, WIDTH / 3, HEIGHT / 20)];
            [self.view addSubview:self.backLabel];
            [self.backLabel release];
            self.backLabel.text = @"没有浏览记录";
            
            self.backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"NoSearch.png"]];
            self.backImage.frame = CGRectMake(self.view.center.x - WIDTH / 6, HEIGHT / 4, WIDTH / 3, HEIGHT / 5);
            [self.view addSubview:self.backImage];
            [self.backImage release];
            
        }

//        [self.tableview reloadData];
    }
    
}



#pragma  mark tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    TuijianCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
    
    static NSString *strs = @"table";
    TuijianCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
    if (!cell) {
        cell = [[TuijianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
    }
     [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉分割线
    GMHSurroundModel* model = self.dataArr[indexPath.row];
    [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://cdn5.jinxidao.com/%@", model.mImageUrl]]];
    cell.title.text = model.productName;
    cell.describe.text = model.productTitleContent;
    cell.money.text = [NSString stringWithFormat:@"￥%@", model.price];
    cell.normalMoney.text = [NSString stringWithFormat:@"￥%@", model.retailPrice];
    cell.sell.text = [NSString stringWithFormat:@"已售%@", model.saledCount];
    NSLog(@"%@", model.saledCount);
    // 字体删除线
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.retailPrice]];
    NSRange contentRange = {0.9,[content length]};
    [content addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:contentRange];
    cell.normalMoney.attributedText = content;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tabBarController.tabBar.hidden = YES;
    GMHFindDetailViewController *detail = [[GMHFindDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    GMHSurroundModel *find = self.dataArr[indexPath.row];
    detail.productIds = find.productId;
    detail.model = find;
}





- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
    [self createDb];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
